package lyq.spec.edu.week14_home04;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private WsReceiver receiver=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(MainActivity.this,MyService.class);
        startService(intent);

        //注册广播接收器
        receiver=new WsReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("lyq.spec.edu.MyService");
        MainActivity.this.registerReceiver(receiver,filter);


        new Thread(new Runnable() {   //开启一个子线程
            @Override
            public void run() {
                try {

                    mTextMessage = (TextView) findViewById(R.id.message);
                    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
                    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();

    }

    @Override
    protected void onDestroy() {
        //结束服务
        stopService(new Intent(MainActivity.this, MyService.class));
        super.onDestroy();
    }

    /**
     * 获取广播数据
     *
     * @author jiqinlin
     *
     */
    public class WsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle=intent.getExtras();
            String ws=bundle.getString("count");
            mTextMessage.setText(ws);
        }
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    new WsReceiver();
                    return true;
            }
            return false;
        }
    };



}
